home *** CD-ROM | disk | FTP | other *** search
- Path: news.jhu.edu!news
- From: Stephanos Androutsellis-Theotokis <stheotok@bme.jhu.edu>
- Newsgroups: comp.lang.c++
- Subject: Question on constructors
- Date: Mon, 22 Jan 1996 14:32:18 -0500
- Organization: Johns Hopkins University
- Message-ID: <3103E642.41C6@bme.jhu.edu>
- NNTP-Posting-Host: 128.220.14.26
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (X11; I; IRIX 5.2 IP22)
-
- I have the following question on constructors:
-
- I wrote a c++ library that uses a dummy non-local static instance of a class
- with a default constructor to force library initialization.
- I thought C++ semantics guaranteed that the constructor for this dummy
- object is called before any other functions in the same "translation unit",
- or, at the very least, before any member functions of this dummy object
- are called.
- Is this assumption wrong?
- If so, how can one force a certain constructor to be called before any
- others?
- If not, why does the following program violate this assumption under my
- Watcom v10.0 compiler?
-
- File z.cpp:
- ////////////////////////////////////////////////////////////////////////////////
- #include "mylib.h"
- #include <iostream.h>
- #include <string.h>
-
- class A {
- public:
- A();
- A(char *as);
- ~A();
- private:
- char *s;
- };
-
- A aa("main global non static");
- static A a("main global static");
-
- A::A(char *as)
- {
-
- cout << "A constructor called for " << as << "\n";
- s = strdup(mylibf1(as));
- cout << "S is now" << s << "\n";
- }
-
- A::~A()
- {
- cout << "A destructor called\n";
- }
-
- main() {
- A c("main local non static");
- static A d("main local static");
- }
- ////////////////////////////////////////////////////////////////////////////////
- File mylib.h
-
- char *
- mylibf1(char *as);
-
- ////////////////////////////////////////////////////////////////////////////////
- File mylib.cpp
-
- #include "mylib.h"
- #include <string.h>
- #include <iostream.h>
- #include <malloc.h>
-
- class B {
- public:
- B();
- ~B();
- char *libf1(char *as);
- private:
- char *s;
- };
-
- static B dummy;
-
- B::B()
- {
- cout << "Constructor for dummy called.\n";
- s = strdup("Initial str");
- }
-
- B::~B()
- {
- cout << "Destructor for dummy called.\n";
- free(s);
- }
-
- char *
- mylibf1(char *as)
- {
- return ::dummy.libf1(as);
- }
-
- char *
- B::libf1(char *as)
- {
- char *tmp;
-
- tmp = s;
- s = strdup(as);
- return tmp;
- }
- ////////////////////////////////////////////////////////////////////////////////
-
- The program crashes because the constructor for dummy is called after the
- constructors for class A objects in file z.cpp,although these objects'
- constructors call (indirectly) member functions of "dummy".
- Note that if the programs are linked the other way round (e.g. changing
- z.cpp to a.cpp) causes the constructors to be executed in the right
- order.
- The question of how to cause a constructor to be called before anything
- else (in any translation unit) also interests me because constructor calling
- sequence determines the sequence in which destructors will be called. In the
- above example, I would like the constructor for "dummy" to be called first
- regardless of whether the constructor for class A objects uses "dummy" members
- or not. The reason for this is that the destructor for A might use "dummy"
- members, hence requiring "dummy" destructor to be called last.
-
- Any help would be appreciated.
-
- Stephanos.
- stheotok@bme.jhu.edu
-